package training.osms.view.category;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import training.osms.controller.category.CategoryController;
import training.osms.model.category.Category;
import training.osms.model.category.CategorySearch;
import training.osms.view.OsmsUtil;

@ManagedBean
@SessionScoped
public class SearchCategoryForm {

	private static final Integer MAX_RESULTS = 3;
	private CategorySearch search;
	private List<Category> result;
	private boolean categorySaved;
	private Category editCategory;
	private List<Integer> pages;
	private Integer page;
	
	public SearchCategoryForm(){
		initialize();
	}

	public String initialize(){
		this.search = new CategorySearch();
		setCategorySaved(false);
		setResult(null);
		setEditCategory(null);
		search.setMaxResults(MAX_RESULTS);
		return "searchCategory";
	}
	
	public CategorySearch getSearch() {
		return search;
	}

	public void setSearch(CategorySearch search) {
		this.search = search;
	}
	
	public void searchCategories(){
		pages = new ArrayList<Integer>();
		CategoryController controller = new CategoryController();
			
		int blogCount = controller.searchCategoryCount(search);
		if(blogCount == 0){
			OsmsUtil.sendMessage("Blog not found.", OsmsUtil.INFO);
		}else{			
			Integer page = 1;
			while(blogCount > 0){
				pages.add(page);
				page++;
				blogCount -= search.getMaxResults();
			}
			goToFirstPage();
		}
		
	}

	public void goToFirstPage() {
		goToPage(1);
	}

	public void goToNextPage() {
		if(getPage() < pages.size()){
			goToPage(getPage()+1);
		}		
	}

	public void goToLastPage() {
		goToPage(pages.size());
		
	}
	public void goToPreviousPage() {
		if(getPage() > 1){
			goToPage(getPage() - 1);
		}
		
		
	}

	public void goToPage(int page) {
		int firstResult = (page-1) * search.getMaxResults();
		search.setFirstResult(firstResult);
		CategoryController controller = new CategoryController();
		result = controller.searchCategory(search);
		this.setPage(page); 
	}

	public List<Category> getResult() {
		return result;
	}

	public void setResult(List<Category> result) {
		this.result = result;
	}
	
	public String editEntity(Category category){
		setEditCategory(category.clone());
		setCategorySaved(false);
		return "editCategory";
	}

	public Category getEditCategory() {
		return editCategory;
	}

	public void setEditCategory(Category editCategory) {
		this.editCategory = editCategory;
	}

	public boolean getCategorySaved() {
		return categorySaved;
	}

	public void setCategorySaved(boolean categorySaved) {
		this.categorySaved = categorySaved;
	}

	public List<Integer> getPages() {
		return pages;
	}

	public void setPages(List<Integer> pages) {
		this.pages = pages;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}
	
	
	
	
}
